package com.gemme.gemme.shequ;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.gemme.gemme.R;
import com.gemme.gemme.SearchActivity;
import com.gemme.gemme.fragment.BaseLazyFragment;
import com.gemme.gemme.home.BrandBean;
import com.gemme.gemme.home.HomeNewFragment;
import com.gemme.gemme.home.ProductListActivity;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.DefineLoadMoreView;
import com.gemme.gemme.view.StaggeredDividerItemDecoration;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by sp on 2019/7/18.
 * 首页
 */
public class BrandFragment extends BaseLazyFragment  {

    View inflate;
    Context mContext;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        inflate = inflater.inflate(R.layout.fragment_brand, container, false);

        mContext = BrandFragment.this.getContext();


        return inflate;
    }


    private SwipeRecyclerView mRecyclerView;
    private List<BrandBean.DataBean> mDataList = new ArrayList<>();
    BrandAdapter mAdapter;
    StaggeredDividerItemDecoration staggeredDividerItemDecoration;
    private void initView(View inflate) {
        mRecyclerView = inflate.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));


      getData();

    }

    private void getData() {
        String url = DataManager.BaseUrl_panghu + "mall/shopbrandinfo/brandInfoList";
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,"Bearer ca19c118-e9f2-46a4-b03f-9950cb1a75dc");
        DataManager.toServer(mContext, url, true, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {

                try {
                    BrandBean brandBean = JSON.parseObject(response, BrandBean.class);
                    List<BrandBean.DataBean> data = brandBean.getData();
                    if(data != null){
                        BrandAdapter brandAdapter =  new BrandAdapter(data);
                        mRecyclerView.setAdapter(brandAdapter);
                        System.out.println();
                    }

                }catch (Exception e){

                }
            }
        });
    }


     class BrandAdapter extends RecyclerView.Adapter<BrandAdapter.ViewHolder>{
        private List<BrandBean.DataBean> mDataList;

        public BrandAdapter(List<BrandBean.DataBean> mDataList){
            this.mDataList = mDataList;
        }

        public void notifyDataSetChanged(List<BrandBean.DataBean> dataList) {
            this.mDataList = dataList;
            super.notifyDataSetChanged();
        }
        @NonNull
        @Override
        public BrandAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.brand_item, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new BrandAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull BrandAdapter.ViewHolder holder, int position) {
          try {
              BrandBean.DataBean dataBean = mDataList.get(position);
              holder.name.setText( dataBean.getBrandName());
              Glide.with(holder.picture).load(dataBean.getBrandLogo()).into(holder.picture);

              holder.itemView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      Intent intent1 = new Intent(getContext(), ProductListActivity.class);
                      intent1.putExtra("BrandId",dataBean.getId() + "");
                      startActivity(intent1);
                  }
              });
          }catch (Exception e){

          }

        }

        @Override
        public int getItemCount() {
            return mDataList.size();
        }


        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView picture;
            TextView name;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                picture = itemView.findViewById(R.id.bigpicture);
                name = itemView.findViewById(R.id.bigname);
            }
        }
    }

    @Override
    public void onLazyLoad() {

        //yc
        initView(inflate);
//        getData();
    }









}
